/**
 * 
 */
package uk.co.lunagroup.vladar.oncenter;
/**
 * 
 * @author Robert Kowalski
 * methods to manipulate cells in 2d String[][] array
 *
 */


public class ArrayOperations {
	/**
	 *copy range of cells from one array to another 
	 *@author Robert Kowalski
	 *@param sourceArray where from we are copy
	 *@param destArray where to copying
	 *@param destY to which row in destination array we will copy
	 *@param destX to which column in destination array we will copy
	 *@param sourceY from which row in source array we will copy
	 *@param sourceX from which column in source array we will copy
	 *@param YSize  how many rows to copy
	 *@param XSize how many columns to copy
	 *@return destArray with copied range
	 */	
	public static String[][] copyCellRange (String[][] sourceArray, String[][] destArray, 
					int destY, int destX, int sourceY, int sourceX, int YSize, int XSize) {
		
		//first go through rows 
		for (int i=0; i<YSize; i++) {
			//now go through columns
			for (int j=0; j<XSize;j++) {
				destArray[destX+j][destY+i]=sourceArray[sourceX+j][sourceY+i];
			}
		}
			
		return destArray;	
	}

	/**
	 * Check number of rows in 2D string array
	 */
	public static int checkArrayXSize (String[][] arrayToCheck) {
		
		int ArrayXSize=0;
		ArrayXSize = arrayToCheck.length;
		return ArrayXSize;
	
	}

	/**
	 * Check number of columns in 2D string array
	 */
	public static int checkArrayYSize (String[][] arrayToCheck) {
		
		int ArrayYSize=0;
		ArrayYSize=arrayToCheck[0].length;
		return ArrayYSize;
	
	}

	/**
	 * copy cell from one array to the other. arrays and positions of copy as parameters
	 * @param inputRowNumber
	 * @param inputColumnNumber
	 * @param outputRowNumber
	 * @param outputColumnNumber
	 * @param inputArray
	 * @param outputArray
	 * @return output array with copied cells
	 * @replaced by copyCellRange
	 * @deprecated 
	 * 
	 */
	public static String[][] copyCell(int inputRowNumber, int inputColumnNumber, int outputRowNumber, int outputColumnNumber, String[][] inputArray, String[][] outputArray){
		
		//to avoid out of bounds excepiton if row is 0 there is nothing to read
		//debug
		//System.out.println(cell);
		outputArray[outputColumnNumber][outputRowNumber]=inputArray[inputColumnNumber][inputRowNumber];
		return outputArray;
	}
	/**
	 * this method delete row and returns array shorter by one row. If used in loop do not forget to change
	 * conditions of the loop. Ex. 
	 * for (int i=0; i<YSize; i++) {
	 * do something .deleteRow(array);
	 * i=i-1;
	 * YSize=YSize-1;
	 * }
	 * @param inputArray
	 * @param row
	 * @return array without row
	 */
	public static String[][] deleteRow (String[][] inputArray, int row){
		int arrayXSize=checkArrayXSize(inputArray);
		int arrayYSize=checkArrayYSize(inputArray);
		
		//new array which will hold new values. Size as original input table
		String [][] outputArray = new String [arrayXSize][arrayYSize-1]; //new table will be one row smaller after deleting row
		
		//copy rows up to row and then o=mit row and copy rest
		outputArray=copyCellRange(inputArray, outputArray, 0, 0, 0, 0, row, arrayXSize);
		outputArray=copyCellRange(inputArray, outputArray, row,0, row+1, 0, arrayYSize-row-1, arrayXSize);
		
		//debug
		//printArray(outputArray);
		//end of debug
			
		return outputArray;
		
	}
	
	/**
	 * method is printing array on standard out
	 * @param array
	 */
	
	public static void printArray (String[][] array) {
		
		int nrOfRows = checkArrayYSize(array);
		int nrOfColumns = checkArrayXSize(array);
			
			for (int i=0; i<nrOfRows;i++) {
				System.out.print("ROW "+i+": ");
				for(int j=0;j<nrOfColumns;j++) {
					System.out.print(array[j][i]+" ");
				}
			System.out.println();
			}
	}
	
	/**
	 * makes a copy of array. Returns array
	 * @param array
	 * @return array
	 */
	public static String[][] cloneArray (String[][] array){
		return array;
	}
	public static String[][] resizeArray (String[][] array, int addColumns, int addRows){
		int XSize = checkArrayXSize(array);
		int YSize = checkArrayYSize(array);
		
		//now create new bigger array
		String [][] newArray = new String[XSize+addColumns][YSize+addRows];
		
		//and now copy the data from oryginal array to new one
		newArray = copyCellRange(array, newArray, 0, 0, 0, 0, YSize, XSize);
		
		return newArray;
	}
	
public static String[][] shrinkArray (String[][] array, int minusColumns, int minusRows) {
	int arrayYSize=checkArrayYSize(array);
	int arrayXSize=checkArrayXSize(array);
	int outputArrayYSize=arrayYSize-minusRows;
	int outputArrayXSize=arrayXSize-minusColumns;
	
	String [][] outputArray = new String[outputArrayXSize][outputArrayYSize];
	outputArray=copyCellRange(array, outputArray, 0, 0, 0, 0, outputArrayYSize, outputArrayXSize);
	return outputArray;
}

	/**
	 * copy row from sourceArray to destArray with destY sourceY parameters
	 * @param sourceArray where from to copy
	 * @param destArray where to copy
	 * @param destY which in destination table should be copied t
	 * @param sourceY which row in source table should be copied from 
	 * @return
	 */
	public static String[][] copyRow (String[][] sourceArray, String[][] destArray, int destY, int sourceY) {
		
		int sourceArrayXSize=checkArrayXSize(sourceArray);
		int destArrayXSize=checkArrayXSize(destArray);
	
		if (sourceArrayXSize != destArrayXSize) {
			System.out.println("Arrays have different column numbers. Can not copy rows between arrays");
			//TODO add handling for if the arrays are not the same size
		}
		destArray=copyCellRange (sourceArray, destArray, destY, 0, sourceY, 0, 1, sourceArrayXSize);
		
		return destArray;
	}


	/**
	  * checking if the cell has a certain value 
	  * if yes returns true
	  * @param cellToTest cell to test
	  * @param valueToCheck string to check
	  * @return true if cell contains string which we are looking for
	  */
	public static boolean compareCell (String cellToTest, String valueToCheck) {
		boolean result = false;
			//debug
			//System.out.print(cellToTest);
		
		//second parameter - ONCenter is returning some cells which looks like empty, but it is not empty.
			if (valueToCheck.equals(cellToTest)) { 
				result=true;
			}
				
		return result;
	}
	
	
}



	
	